package org.numanint.code;

import java.util.ArrayList;
import java.util.HashMap;


public class ValidatorGenerator extends Generator {
	
	public ValidatorGenerator(GeneratorProperties p){
		super(p);
	}
	public void generator(ReverseEngineer reveng) throws Exception{	
			HashMap<String,Object> data = new HashMap<String,Object>();
			for(int i=0;i<reveng.getTableClasss().length;i++){
				String className = ConventUtils.t2c(reveng.getTableClasss()[i].getTableName());		
				ArrayList<TablePKClass> pk = reveng.getTableClasss()[i].getPkArray();
				ArrayList<TableKeyClass> importKeyArray = reveng.getTableClasss()[i].getImportKeyArray();
				ArrayList<TableKeyClass> exportKeyArray = reveng.getTableClasss()[i].getExportKeyArray();
				ArrayList<TableFieldClass> fieldArray = new ArrayList<TableFieldClass>();
				HashMap<String,Object> tableMap = new HashMap<String,Object>();
				tableMap.put("properties", getProperties());
				tableMap.put("className", className);		
				tableMap.put("importKey",importKeyArray);
				tableMap.put("exportKey",exportKeyArray);
				tableMap.put("pk", pk.get(0).getPkClassName());
				for(int j=0;j<reveng.getTableClasss()[i].getFields().length;j++){
					TableFieldClass tableFeild = new TableFieldClass();
					tableFeild = (TableFieldClass)reveng.getTableClasss()[i].getFields()[j].clone();
					boolean flag = false;
					if(pk.get(0).getPkColumnName().compareTo(tableFeild.getName())==0) flag = true;
					else
				    for(int k=0;k<importKeyArray.size();k++){
				        if(importKeyArray.get(k).getKeyColumnName().compareTo(tableFeild.getName())==0){
				        	flag = true;break;
				        }
				    }
				    if(flag) continue; 
					fieldArray.add(tableFeild);
				}
				tableMap.put("fields", fieldArray);
				data.put(className+"Validator.java", tableMap);
			} 
			if(getProperties().getControllerPkgName()!=null) 
				setOutputDir(getOutputDir()+getProperties().getValidatorPkgName().replace('.',System.getProperty("file.separator").charAt(0)));

			super.setData(data);
			super.generator();
		
	}	
}
